package com.obor.quartz.job.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zn.mvcbase.mybatis.ExampleProvider;
import org.zn.mvcbase.page.PaginationUtility;
import org.zn.mvcbase.page.Pagnation;

import com.obor.quartz.job.JobInfo;
import com.obor.quartz.job.mapper.JobInfoMaper;

import tk.mybatis.mapper.entity.Example;

/**
*
* @Company OBOR
* @Author 李志鹏
* @Description
* @Date 2019年4月25日
*
*/
@Service
public class JobInfoService {

	@Autowired
	private JobInfoMaper jobInfoMaper;
	
	/**
	 * 新增修改
	 * @param jobInfo
	 */
	public void add(JobInfo jobInfo) {
		if(StringUtils.isNoneBlank(jobInfo.getId())) {
			jobInfo.setUpdateTime(new Date());
			jobInfoMaper.updateByPrimaryKeySelective(jobInfo);
		}else {
			jobInfoMaper.insertSelective(jobInfo);
		}
	}
	
	/**
	 * 列表查询
	 * @param page
	 * @param jobInfo
	 * @return
	 */
	@Pagnation
	public List<JobInfo> list(PaginationUtility<?> page,JobInfo jobInfo){
		jobInfo.setDataStatus("valid");
		Example example =ExampleProvider.wrapperEntity(jobInfo).provider();
		
		return jobInfoMaper.selectByExample(example);	
	}
	
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	public JobInfo one(String id) {
		return jobInfoMaper.selectByPrimaryKey(id);
	}
	
	/**
	 * 删除
	 * @param ids
	 */
	@Transactional(rollbackFor = RuntimeException.class)
	public void del(List<String> ids) {
		boolean flag = ids.stream().filter(StringUtils::isNoneBlank).allMatch(x -> jobInfoMaper.updateByPrimaryKeySelective(new JobInfo(x,"invalid")) == 1);
		
		if(!flag) {
			throw new RuntimeException("操作失败");
		}
	}
}
